package com.gpframework.module.process.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.gpframework.module.manufacture.domain.GpProductionDetails;
import com.gpframework.module.manufacture.domain.req.GpProductionDetailsUpdate;
import com.gpframework.module.process.domain.GpProcessDeliveryNote;
import com.gpframework.module.process.domain.req.GpProcessOverflowRateUpdate;
import com.gpframework.module.process.service.IGpProcessDeliveryNoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gpframework.module.process.mapper.GpProcessOverflowRateMapper;
import com.gpframework.module.process.domain.GpProcessOverflowRate;
import com.gpframework.module.process.domain.vo.GpProcessOverflowRateVo;
import com.gpframework.module.process.service.IGpProcessOverflowRateService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
/**
 * @ClassName GpProcessOverflowRate
 * @Author gpframework
 * @Date 2023-12-20
 * @Version 1.0.0
 * @Description 溢码率Service业务层处理
 */
@Service
public class GpProcessOverflowRateServiceImpl extends ServiceImpl<GpProcessOverflowRateMapper, GpProcessOverflowRate> implements IGpProcessOverflowRateService {

    @Autowired
    private IGpProcessDeliveryNoteService iGpProcessDeliveryNoteService;

    /**
     * @Description 查询列表返回VO 用于返回给前端的列表接口
     * @Author gpframework
     * @Date 2023-12-20
     * @Param [lqw]
     * @Return List<GpProcessOverflowRateVo>
     */
    @Override
    public List<GpProcessOverflowRateVo> listVo(QueryWrapper<GpProcessOverflowRate> lqw) {
        return this.baseMapper.listVo(lqw);
    }

    /**
     * @Description 通过查询详情VO 用于返回给前端的列详情接口
     * @Author gpframework
     * @Date 2023-12-20
     * @Param [id]
     * @Return GpProcessOverflowRateVo
     */
    @Override
    public GpProcessOverflowRateVo getVoById(Long id) {
        return this.baseMapper.getVo(id);
    }

    @Override
    public boolean edit(List<GpProcessOverflowRateUpdate> list) {
        ArrayList<GpProcessOverflowRate> gpProcessOverflowRateUpdates = new ArrayList<>();
        for (GpProcessOverflowRateUpdate gpProcessOverflowRateUpdate : list) {
            GpProcessOverflowRate gpProcessOverflowRate = Convert.convert(new TypeReference<GpProcessOverflowRate>() {
            }, gpProcessOverflowRateUpdate);

            if (gpProcessOverflowRate.getOutboundBacker() != null && gpProcessOverflowRate.getConvertBacker()!=null)  {
                //计算   出码率换算 （足码数）/底布出去的总足码数
                gpProcessOverflowRate.setOutputRate(gpProcessOverflowRate.getOutboundBacker().multiply(gpProcessOverflowRate.getConvertBacker()));
            }
            if (gpProcessOverflowRate.getProcessDeliveryNoteId() != null) {
                GpProcessDeliveryNote gpProcessDeliveryNote = iGpProcessDeliveryNoteService.getById(gpProcessOverflowRate.getProcessDeliveryNoteId());
                if (gpProcessDeliveryNote != null && gpProcessDeliveryNote.getTotalSizeSufficient() != null) {
                    gpProcessOverflowRate.setProductReturn(BigDecimal.valueOf(gpProcessDeliveryNote.getTotalSizeSufficient()));
                }
            }

            if (gpProcessOverflowRate.getOutputRate() != null && gpProcessOverflowRate.getProductReturn() != null) {
                //溢码数（差异数）  加工后的成品总数 -底布换算的总数
                //差异率  （溢码率） 溢码数（差异数）/出码率换算 （足码数）
                gpProcessOverflowRate.setDifferenceNum(gpProcessOverflowRate.getProductReturn().subtract(gpProcessOverflowRate.getOutputRate()));
                if (gpProcessOverflowRate.getProductReturn().compareTo(BigDecimal.ZERO) != 0) {
                    gpProcessOverflowRate.setDifferenceRate(gpProcessOverflowRate.getDifferenceNum().divide(gpProcessOverflowRate.getOutputRate(), 2, RoundingMode.HALF_UP));
                }
            }
            gpProcessOverflowRateUpdates.add(gpProcessOverflowRate);
        }

        return this.updateBatchById(gpProcessOverflowRateUpdates);
    }

    @Override
    public boolean copy(Long id, Integer copyNum) {
        GpProcessOverflowRate gpProcessOverflowRate = this.getById(id);
        if (gpProcessOverflowRate == null) {
            return false;
        }
        for (int i = 0; i < copyNum; i++) {
            gpProcessOverflowRate.setId(null);
            this.save(gpProcessOverflowRate);
        }
        return true;
    }

}
